/**
 * 
 */
package mm.front.jsf;

import java.util.Iterator;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;


/**
 * @author PUSDERKIS
 *
 */
public class JSFUtils {

	private JSFUtils(){
		
	}
	
	/**
	   * Finds component with the given id
	   */
	  public static UIComponent findComponent(UIComponent c, String id) {
	    if (id.equals(c.getId())) {
	      return c;
	    }
	    Iterator<UIComponent> kids = c.getFacetsAndChildren();
	    while (kids.hasNext()) {
	      UIComponent found = findComponent(kids.next(), id);
	      if (found != null) {
	        return found;
	      }
	    }
	    return null;
	  }
	
	  public static void addInfoMessage(String clientId, String msg){
			FacesMessage fm = new FacesMessage(msg);
			fm.setSeverity(FacesMessage.SEVERITY_INFO);
			
			FacesContext.getCurrentInstance().addMessage(clientId, fm);
	  }
	  
	  
	  public static void addInfoMessage(String msg){
		  addInfoMessage(null, msg);
	  }
	  
	  public static void addErrorMessage(String clientId, String msg){
			FacesMessage fm = new FacesMessage(msg);
			fm.setSeverity(FacesMessage.SEVERITY_ERROR);
			
			FacesContext.getCurrentInstance().addMessage(clientId, fm);
	  }
	  
	  
	  public static void addErrorMessage(String msg){
		  addErrorMessage(null, msg);
	  }
	  
	  
}
